//
//  WSNewsDetailViewController.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/31.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSNewsDetailViewController.h"
#import "WSCommentViewController.h"
#import "WSNewsDetailHeader.h"
#import "WSCommentCell.h"

#import "NewsService.h"
#import "WSNewsDetailInfo.h"
#import "WSMessageInfo.h"

@interface WSNewsDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *detailTable;

@property (nonatomic, strong) WSNewsDetailHeader *headerView;
@property (nonatomic, strong) UIView *sectionHeader;

// 回复区域高度
@property (nonatomic, assign) CGFloat replyAreaHeight;

@property (nonatomic, strong) NSArray <WSMessageInfo *> *commentList;

@end

@implementation WSNewsDetailViewController

#pragma mark - Life cycle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self ws_setup];
    [self ws_loadData];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.headerView.xmg_height = self.headerView.contentHeight;
}

- (void)ws_setup {
    
    // table view header
    WSNewsDetailHeader *header = [WSNewsDetailHeader viewFromXib];
    self.headerView = header;
    self.detailTable.tableHeaderView = header;
    self.detailTable.tableFooterView = [UIView new];
    
    // regist cells
    [self.detailTable registerNib:[UINib nibWithNibName:[WSCommentCell className] bundle:nil] forCellReuseIdentifier:[WSCommentCell className]];
}

/**
 获取资讯详情数据
 */
- (void)ws_loadData {
    [self showHud];
    [NewsService fetchNewsDetailWithId:_articleId callback:^(ResponseModel * _Nonnull response) {
        [self hideHud];
        // 资讯详情
        WSNewsDetailInfo *detailInfo = [WSNewsDetailInfo yy_modelWithJSON:response.data[@"article"]];
        self.headerView.detailInfo = detailInfo;
        // 评论内容
        self.commentList = [NSArray yy_modelArrayWithClass:[WSMessageInfo class] json:response.data[@"msg_list"]];
        [self.detailTable reloadData];
    }];
}

#pragma mark - Action

// 点击发布评论按钮
- (IBAction)sendCommentAction:(id)sender {
    WSCommentViewController *commentVc = [[WSCommentViewController alloc] init];
    commentVc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    commentVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    // call back
    WEAKSELF
    commentVc.sendMsgCallback = ^(NSString * _Nonnull msg) {
        [NewsService addCommentWithMsg:msg andNewsId:weakSelf.articleId Completion:^(BOOL success) {
            if (success) {
                [weakSelf ws_loadData];
            } else {
                [weakSelf showHint:@"评论失败"];
            }
        }];
    };
    
    [self presentViewController:commentVc animated:YES completion:nil];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.commentList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:[WSCommentCell className] forIndexPath:indexPath];
    cell.messageInfo = self.commentList[indexPath.section];
    // 点赞
    WEAKSELF
    cell.didTapLikeCallback = ^(UIButton * _Nonnull btn, WSCommentCell * _Nonnull aCell) {
        WSMessageInfo *msg = weakSelf.commentList[indexPath.section];
        [NewsService likeHandleWithMsgId:msg.msg_id andNewsId:weakSelf.articleId Completion:^(BOOL success) {
            if (success) {
                msg.zan += 1;
                aCell.messageInfo = msg;
            }
        }];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}


#pragma mark - Getter

- (UIView *)sectionHeader {
    if (_sectionHeader == nil) {
        _sectionHeader = [UIView new];
        _sectionHeader.backgroundColor = [UIColor whiteColor];
        [_sectionHeader setBorder:kSeparatorColor borderWidth:1.f borderType:UIBorderSideTypeBottom];
        
        UIView *greenLine = [UIView new];
        greenLine.backgroundColor = kThemeGreenColor;
        greenLine.layer.cornerRadius = 1.f;
        [_sectionHeader addSubview:greenLine];
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = @"评论";
        titleLabel.textColor = COLOR_HEX(0x191919);
        titleLabel.font = WSFontMake(15);
        [_sectionHeader addSubview:titleLabel];
        
        [greenLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(11.f);
            make.centerY.offset(0.f);
            make.size.mas_equalTo(CGSizeMake(3.f, 18.f));
        }];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(greenLine).offset(10.f);
            make.centerY.offset(0.f);
        }];
    }
    return _sectionHeader;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
